package com.hxzy.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import com.hxzy.config.interceptor.AdminTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 配置springmvc的一些常用属性
 */
@Configuration
public class SpringMvcConfig extends WebMvcConfigurationSupport {


    /**
     * 后台拦截器
     */
    @Autowired
    private AdminTokenInterceptor  adminTokenInterceptor;

    /**
     * 自定义拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(this.adminTokenInterceptor).addPathPatterns("/api/**")
               .excludePathPatterns("/api/login");

        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
    }

    /**
     * 静态资源放行( css    images    js )
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");

        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("swagger-ui.html", "doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

    }




}
